import axios from 'axios'

export function fetch (url,parameObj={},method='POST'){
    return new Promise((resolve,reject)=>{
        var postParameObj = {}
        var getParameObj = {}
        if(method.toUpperCase()==='POST' || method.toUpperCase()==='PUT'){
            postParameObj = parameObj
        }else{
            getParameObj = parameObj
        }
        let headers = {}
        if(parameObj.headers){
            headers = parameObj.headers
        }
        headers['X-Auth-Value'] = ''
        headers['X-Emp-No'] = ''
        axios({
            method:method,
            url:url,
            data:postParameObj,
            params:getParameObj,
            headers:headers
        }).then((response) => {
            if(response.data.code.code === '0000'){
                resolve(response.data)
            }else if(response.data.code.code === '0002'){
                const domin = window.location.href
            }else{
                reject(response.data.code)
            }
        }).catch((error)=>{
            reject(error)
        })
    })
}


axios.interceptors.request.use(
    config => {
        const token = getCookie('PORTALSSOCookie') || getCookie('ZTEDPGSSOCookie')
        if(tokne){
            config.headers['X-Auth-Value'] = encodeURIComponent(tokne)
            config.headers['X-Emp-No'] = getCookie('PORTALSSOUser') || getCookie('ZTEDPGSSOUser')
            config.headers['X-Lang-Id'] = getXLangId()
        }
        return config
    },
    err =>{
        return Promise.reject(err)
    }
)